<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Control : historibayar  * di Buat oleh Diar PHP Generator  *  By Diar */

class ctrlaporanpembayaran extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index($xAwal = 0, $xSearch = '') {
//  $this->load->view('test.php');
        $idpegawai = $this->session->userdata('idpegawai');
        if (empty($idpegawai)) {
            redirect(site_url(), '');
        }
        if ($xAwal <= -1) {
            $xAwal = 0;
        } $this->session->set_userdata('awal', $xAwal);
        $this->createformhistoribayar('0', $xAwal);
    }

    function createformhistoribayar($xidx, $xAwal = 0, $xSearch = '') {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('modelgetmenu');
        $xAddJs = '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/baseurl.js"></script>' .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/ajaxlaporanpembayaran.js"></script>';
        echo $this->modelgetmenu->SetViewAdmin($this->setDetailFormhistoribayar($xidx), '', '', $xAddJs, '');
    }

    function setDetailFormhistoribayar($xidx) {
        $this->load->helper('form');
        
        $xBufResult = '';
        $xBufResult = '<div id="stylized" class="myform"><h3>Laporan Pembayaran</h3><div class="garis"></div>' . form_open_multipart('ctrhistoribayar/inserttable', array('id' => 'form', 'name' => 'form'));
        $this->load->helper('common');
        $xBufResult .= '<input type="hidden" name="edidx" id="edidx" value="0" />';
//        $xBufResult .= setForm('edidcustomer', 'idcustomer', form_input(getArrayObj('edidcustomer', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edidpaket', 'idpaket', form_input(getArrayObj('edidpaket', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edjenisbayar', 'jenisbayar', form_input(getArrayObj('edjenisbayar', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edtotal', 'total', form_input(getArrayObj('edtotal', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('eddate_mulai', 'Tanggal pembayaran', form_input(getArrayObj('eddate_mulai', '', '100')));
        $xBufResult .= setForm('eddate_akhir', ' ', form_input(getArrayObj('eddate_akhir', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edstatus', 'Status Bayar', form_dropdown('edstatus', array('0'=>'Tidak Aktif','1'=>'Aktif','2'=>'Pannding'), '', ' id="edstatus" onChange="dosearchcustomer(\'0\');" class="require" style="width:200px;" ')) . '<div class="spacer"></div>';
        //        $xBufResult .= setForm('edidpegawai', 'idpegawai', form_input(getArrayObj('edidpegawai', '', '100'))) . '<div class="spacer"></div>';
        //$xBufResult .= '<div class="garis"></div>' . form_button('btSimpan', 'simpan', 'onclick="dosimpanhistoribayar();"') . form_button('btNew', 'new', 'onclick="doClearhistoribayar();"') . '<div class="spacer"></div><div id="tabledatacustomer">' . $this->getlistcustomer(0, '') . '</div><div class="spacer"></div>';
        $xBufResult .= '<div class="spacer"></div><div id="tabledatacustomer">' . $this->getlistcustomer(0, '','','',0) . '</div><div class="spacer"></div>';
        return $xBufResult;
    }

    function getlisthistoribayar($xAwal, $xSearch,$xdate_mulai,$xdate_akhir) {
        $xLimit = 10;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('No', '', 'width=10%') .
                tbaddcell('Customer', '', 'width=10%') .
                tbaddcell('Event', '', 'width=10%') .
                tbaddcell('Status', '', 'width=10%') .
                tbaddcell('total', '', 'width=10%') .
                tbaddcell('date_bayar', '', 'width=10%'));
                //tbaddcell('idpegawai', '', 'width=10%') .
             //   tbaddcell('Edit/Hapus', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelhistoribayar');
        $this->load->model('modelcustomer');
        $xQuery = $this->modelhistoribayar->getListhistoribayarbydate($xAwal, $xLimit, $xSearch,$xdate_mulai,$xdate_akhir);
        //print_r($xQuery);
        $no = 1;
        foreach ($xQuery->result() as $row) {
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/edit.png" alt="Edit Data" onclick = "doedithistoribayar(\'' . $row->idx . '\');" style="border:none;width:20px"/>';
            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapushistoribayar(\'' . $row->idx . '\',\'' . substr($row->idcustomer, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($no) .
                   // tbaddcell($this->modelcustomer->getDetailnamacustomer($row->idcustomer)) .
                    tbaddcell($row->idpaket) .
                    tbaddcell($row->jenisbayar) .
                    tbaddcell($row->total) .
                    tbaddcell($row->date_bayar) .
                    tbaddcell($row->idpegawai) .
                    tbaddcell($xButtonEdit . '&nbsp/&nbsp' . $xButtonHapus));
            $no++;
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchhistoribayar(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchhistoribayar(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchhistoribayar(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function editrechistoribayar() {
        $xIdEdit = $_POST['edidx'];
        $this->load->model('modelhistoribayar');
        $row = $this->modelhistoribayar->getDetailhistoribayar($xIdEdit);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['idcustomer'] = $row->idcustomer;
        $this->json_data['idpaket'] = $row->idpaket;
        $this->json_data['jenisbayar'] = $row->jenisbayar;
        $this->json_data['total'] = $row->total;
        $this->json_data['date_bayar'] = $row->date_bayar;
        $this->json_data['idpegawai'] = $row->idpegawai;
        echo json_encode($this->json_data);
    }

    function deletetablehistoribayar() {
        $edidx = $_POST['edidx'];
        $this->load->model('modelhistoribayar');
        $this->modelhistoribayar->setDeletehistoribayar($edidx);
        $this->load->helper('json');
        echo json_encode(null);
    }

    function searchhistoribayar() {
        $this->load->helper('common');
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $xdate_mulai = $_POST['date_mulai'];
        $xdate_akhir = $_POST['date_akhir'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatahistoribayar'] = $this->getlisthistoribayar($xAwal, $xSearch,datetomysql($xdate_mulai),datetomysql($xdate_akhir));
        echo json_encode($this->json_data);
    }

    function simpanhistoribayar() {
        $this->load->helper('json');
        if (!empty($_POST['edidx'])) {
            $xidx = $_POST['edidx'];
        } else {
            $xidx = '0';
        }
        $this->load->helper('common');
        $xidcustomer = $_POST['edidcustomer'];
        $xidpaket = $_POST['edidpaket'];
        $xjenisbayar = $_POST['edjenisbayar'];
        $xtotal = $_POST['edtotal'];
        $xdate_bayar = $_POST['eddate_bayar'];
        $xidpegawai = $_POST['edidpegawai'];
        $this->load->model('modelhistoribayar');
        $idpegawai = $this->session->userdata('idpegawai');
        if (!empty($idpegawai)) {
//            if ($xidx != '0') {
//                $xStr = $this->modelhistoribayar->setUpdatehistoribayar($xidx, $xidcustomer, $xidpaket, $xjenisbayar, $xtotal, date`$xdate_bayar, $xidpegawai);
//            } else {
            $xStr = $this->modelhistoribayar->setInserthistoribayar($xidx, $xidcustomer, $xidpaket, $xjenisbayar, $xtotal, datetomysql($xdate_bayar), $idpegawai);
            //}
        }
        echo json_encode(null);
    }

    ///////////////////////////////////

    function getlistcustomer($xAwal, $xSearch,$date_mulai,$date_akhir,$xidstatus) {
        $xLimit = 20;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('no', '', 'width=5%') .
                tbaddcell('Name', '', 'width=25%') .
                tbaddcell('Paket Event', '', 'width=20%') .
                tbaddcell('Nominal', '', 'width=10%') .
                tbaddcell('Date', '', 'width=10%') .
                tbaddcell('Discription', '', 'width=30%'), '', TRUE);
                //tbaddcell('Save', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelcustomer');
        $this->load->model('modelpaket');
        $this->load->model('modelhistoribayar');
        $xQuery = $this->modelcustomer->getListcustomerbydate($xAwal, $xLimit, $xSearch,$date_mulai,$date_akhir,$xidstatus);
        $no = 1;
        foreach ($xQuery->result() as $row) {
            $rowlast = $this->modelhistoribayar->getDetailhistoribayarbyid($row->idx);
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/btnsave.jpg"  alt="Edit Data" onclick = "dosimpanhistoribayar(\'' . $row->idx . '\');" style="border:none;width:40px;height:40px;"/>';
//            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapuscustomer(\'' . $row->idx . '\',\'' . substr($row->title, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($no) .
                    tbaddcell($row->title . ' ' . $row->firstname . ' ' . $row->midlename . ' ' . $row->lastname) .
                    tbaddcell($this->modelpaket->getDetailpaket($rowlast->idpaket)->namapaket) .
                    tbaddcell($rowlast->total,"text-align:right") .
                    tbaddcell(datetomysql(@$rowlast->date_bayar)) .
                    tbaddcell(@$rowlast->jenisbayar));
                   // tbaddcell($xButtonEdit));
            $no++;
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchcustomer(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function dosethargapaket() {
        $xidpaket = $_POST['edidpaket'];
        $this->load->helper('json');
        $this->load->model('modelpaket');
        $rowpaket = $this->modelpaket->getDetailpaket($xidpaket);
        $this->json_data['hrgpaket'] = $rowpaket->harga;
        echo json_encode($this->json_data);
    }

    function searchcustomer() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $xdate_mulai = $_POST['date_mulai'];
        $xdate_akhir = $_POST['date_akhir'];
        $xidstatus = $_POST['xstatus'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatacustomer'] = $this->getlistcustomer($xAwal, $xSearch,$xdate_mulai,$xdate_akhir,$xidstatus);
        echo json_encode($this->json_data);
    }

}

?>